package com.itheima.article.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.article.pojo.ApArticleConfig;
import com.itheima.article.service.ApArticleConfigService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * kafka消费者监听器
 */
@Component
@Slf4j
public class ApArticleConfigListener {

    @Autowired
    private ApArticleConfigService apArticleConfigService;
    /**
     * 文章与自媒体端上下架同步数据
     * @param record
     */
    //传过来的数据有文章id和上下线的值
    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_TOPIC)
    public void upOrDown(ConsumerRecord<String,String> record){
        //获取消息信息
        String topic = record.topic();
        int partition = record.partition();
        String value = record.value();
        log.info("文章上下架开始：topic={},partition={},value={}",topic,partition,value);

        //解析消息内容
        JSONObject jsonObject = JSON.parseObject(value);
        //获取文章id
        Long articleId = jsonObject.getLong("articleId");
        //获取上下架的值
        Integer enable = jsonObject.getInteger("enable");

        //构建条件构造器
        LambdaUpdateWrapper<ApArticleConfig> lqw = new LambdaUpdateWrapper<>();
        lqw.set(ApArticleConfig::getIsDown,enable);
        lqw.eq(ApArticleConfig::getArticleId,articleId);

        if(apArticleConfigService.update(lqw)){
            log.info("文章上下架同步成功：topic={},partition={},value={}",topic,partition,value);
        }else{
            log.info("文章上下架同步失败：topic={},partition={},value={}",topic,partition,value);
        }
    }

    /**
     * 删除文章同步文章配置表
     * @param record
     */
    //传过来的数据有文章id和是否删除
    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_IS_DELETE_TOPIC)
    public void isDelete(ConsumerRecord<String,String> record){
        //获取消息信息
        String topic = record.topic();
        int partition = record.partition();
        String value = record.value();
        log.info("文章上下架开始：topic={},partition={},value={}",topic,partition,value);

        //解析消息内容
        JSONObject jsonObject = JSON.parseObject(value);
        //获取文章id
        Long articleId = jsonObject.getLong("articleId");
        //获取上下架的值
        Integer isDelete = jsonObject.getInteger("isDelete");

        //构建条件构造器
        LambdaUpdateWrapper<ApArticleConfig> lqw = new LambdaUpdateWrapper<>();
        lqw.set(ApArticleConfig::getIsDelete,isDelete);
        lqw.eq(ApArticleConfig::getArticleId,articleId);

        if(apArticleConfigService.update(lqw)){
            log.info("文章删除同步成功：topic={},partition={},value={}",topic,partition,value);
        }else{
            log.info("文章删除同步失败：topic={},partition={},value={}",topic,partition,value);
        }
    }
}
